package com.hqd.sellrelation.server.controller.visit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hqd.common.model.PageParams;
import com.hqd.common.model.ResultBody;
import com.hqd.sellrelation.server.dto.VisitDto;
import com.hqd.sellrelation.server.dto.VisitSearchDto;
import com.hqd.sellrelation.server.entity.VisitInfo;
import com.hqd.sellrelation.server.service.VisitInfoService;
import com.hqd.sellrelation.server.vo.VisitInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/visit")
@Api(value = "VisitInfoController", tags = "CRM拜访")
public class VisitInfoController {
    @Autowired
    private VisitInfoService visitInfoService;

    /**
     * 拜访分页查询
     *
     * @return
     */
    @ApiOperation(value = "拜访分页查询", notes = "拜访分页查询")
    @GetMapping("/get")
    public ResultBody<IPage<VisitInfo>> get(PageParams pageParams, VisitSearchDto dto) {
        return ResultBody.ok().data(visitInfoService.findListPage(pageParams,dto));
    }

    /**
     * 编辑或保存
     * @return
     */
    @ApiOperation(value = "编辑或保存", notes = "编辑或保存")
    @GetMapping("/saveOrUpdate")
    public ResultBody<VisitInfoVo> saveOrUpdate(VisitDto dto) {
        try {
            visitInfoService.updateOrSave(dto);
        }catch (Exception e){
            return ResultBody.failed().data("操作失败");
        }
        return ResultBody.ok().data("操作成功");
    }

    /**
     * 取消拜访
     * @return
     */
    @ApiOperation(value = "取消拜访", notes = "取消拜访")
    @GetMapping("/cancel")
    public ResultBody cancel(String visitId) {
        try {
            visitInfoService.cancelVisit(visitId);
        }catch (Exception e){
            return ResultBody.failed().data("操作失败");
        }
        return ResultBody.ok().data("操作成功");
    }
}
